package cn.laixueit.controller;

import cn.laixueit.pojo.item.Category;
import cn.laixueit.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/findall")
    public  ResponseEntity<Collection<Category>>  findAll(){
        List<Category> list = categoryService.list();
        return ResponseEntity.ok(list);
    }

    /**
     * 根据父id查询子节点
     * @param pid
     * @return
     */
    @GetMapping("/list")
    public  ResponseEntity<Collection<Category>> findCategoryListByParentId(@RequestParam(value = "pid",defaultValue = "0")Long pid){
        HashMap<String, Object> map = new HashMap<>();
        map.put("parent_id",pid);
        Collection<Category> categories = categoryService.listByMap(map);
        return ResponseEntity.ok(categories);
    }


    //根据分类id查询分类名称
    @GetMapping("/names")
    public ResponseEntity<List<String>> names(@RequestParam("ids")List<Long> ids){
        List<String> names = categoryService.findNamesByIds(ids);
        return ResponseEntity.ok(names);
    }
}
